package dbAccess;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.sql.*;
public class Admin 
{
	private final static String defaultStr = "unknown";
	private int AdminId;
	private int SIN;
	private String FirstName;
	private String LastName;
	private String EmailAddress;
	private String PhoneNum;
	private String HomeAddress;
	private String password;
	private java.sql.Date DateOfBirth;
	private String gender;
	
	private String statusMsg;
	private String creator;
	
	public Admin(String creatorIn)
	{
		AdminId = -1;
		SIN = -1;
		FirstName = defaultStr;
		LastName = defaultStr;
		EmailAddress = defaultStr;
		PhoneNum = defaultStr;
		password = defaultStr;
		DateOfBirth = java.sql.Date.valueOf("0000-00-00");
		gender = defaultStr;
		statusMsg = "";
		creator = creatorIn;
	}
	
	//creates a new patient or updates existing one
	public Boolean registerAdmin(String action)
	{
		if(action.equals("update"))
		{
			int adminId = this.getAdminId();
			String updateQuery = "update tbl_admin set IsDeleted = '1' where AdminId = " + adminId ;
			DbAccess access = new DbAccess();
			access.ExecuteUpdate(access.connect(), updateQuery);
			Login.Log(creator, "update tbl_admin set IsDeleted = '1' where AdminId = " + adminId);
		}
		
		this.setStatusMsg("");
		//check if SIN or email address already exists in tbl_person
		DbAccess access = new DbAccess();
		String query = "select SIN, EmailAddress from tbl_admin where (SIN = " + SIN + " or EmailAddress = '" + EmailAddress + "'";
		query += ") and IsDeleted = '0' ";
		
		try 
		{
		ResultSet rs = access.ExecuteSelect(access.connect(), query);
		if (rs.next())
		{
			this.setStatusMsg("Admin already exists.");
			return false;
		}
		}
		catch (SQLException e)
		{
			this.setStatusMsg("Duplication check error occurred: " + e.getMessage());
			return false;
		}
			
		//insert into tbl_patient
		query = "insert into tbl_admin ";
		query += "(EmailAddress, SIN, FirstName, LastName, Gender, DateOfBirth, Phone, HomeAddress, ";
		query += "Password, IsDeleted) ";
		query += "values ('" + EmailAddress + "'," + SIN + ",'" + FirstName + "','" + LastName + "','" + gender + "','" + DateOfBirth + "','" + PhoneNum;
		query += "','" + HomeAddress + "','" + password + "',0)";
		
		if (!(access.ExecuteUpdate(access.connect(), query)))
		{
			this.setStatusMsg("Could not update tbl_admin with admin information.");
			return false;
		}
		else
		{
			Login.Log(creator, query);
		}
		
		int oldAdminId = this.getAdminId();
		//store patient ID field
		if (!(evalAdminId()))
		{
			return false;
		}
		
		if(action.equals("update"))
		{	
			this.setStatusMsg(this.getFirstName() + " " + this.getLastName() + " updated with new ID: " + this.getAdminId());
			return true;
		}
		this.setStatusMsg("New Admin created with ID: " + this.getAdminId());
		return true;
	}
	
	private Boolean evalAdminId()
	{
		DbAccess access = new DbAccess();
		String query = "select tbl_admin.AdminId from tbl_admin where tbl_admin.SIN = " + this.SIN + " and ";
		query += "tbl_admin.IsDeleted = 0";
		
		try
		{
			ResultSet rs = access.ExecuteSelect(access.connect(), query);
			rs.next();
			this.AdminId = rs.getInt("tbl_admin.AdminId");
			return true;
		}
		catch (SQLException e)
		{
			return false;
		}	
	}

	public int getAdminId() {
		return AdminId;
	}

	public void setAdminId(int adminId) {
		this.AdminId = adminId;
	}

	public int getSIN() {
		return SIN;
	}

	public void setSIN(int sin) {
		SIN = sin;
	}

	public String getFirstName() {
		return FirstName;
	}

	public void setFirstName(String firstName) {
		FirstName = firstName;
	}

	public String getLastName() {
		return LastName;
	}

	public void setLastName(String lastName) {
		LastName = lastName;
	}

	public String getEmailAddress() {
		return EmailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		EmailAddress = emailAddress;
	}

	public String getPhoneNum() {
		return PhoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		PhoneNum = phoneNum;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public java.sql.Date getDateOfBirth() {
		return DateOfBirth;
	}

	public void setDateOfBirth(java.sql.Date DateOfBirth) {
		this.DateOfBirth = DateOfBirth;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getStatusMsg() {
		return statusMsg;
	}

	public void setStatusMsg(String errorMsg) {
		this.statusMsg = errorMsg;
	}

	public String getHomeAddress() {
		return HomeAddress;
	}

	public void setHomeAddress(String homeAddress) {
		HomeAddress = homeAddress;
	}

}
